// e.r(t),
//   e.d(t, {
//     ModelRatedMessage: () => o.E,
//     SubmitModelRatingCommand: () => SubmitModelRatingCommand,
//     ToggleModelRatingDialogCommand: () => ToggleModelRatingDialogCommand,
//     default: () => w,
//   });
import Module from "./Module"
import Logger from "../utils/Logger"
import ModelRatingViewData from "../data/ModelRatingViewData"
import { SubmitModelRatingCommand, ToggleModelRatingDialogCommand } from "../command/rating.command"
import { ToggleModalCommand } from "../command/modal.command"
import modalEnum from "../enum/modal.enum"
import Store from "../storage/Store"
const l = new Logger("model-rating-data-store"),
  h = i => i,
  c = i => i
class ModelRatingDataStore extends Store.JsonStore {
  constructor(i, t, e) {
    super({
      queue: i,
      deserialize: h,
      serialize: c,
      path: `${t}/api/v1/jsonstore/model/model-rating/${e}`
    })
  }
  async canPromptRating() {
    let i
    try {
      i = await this.read()
    } catch (i) {
      return l.error("Failed to read existing rating data from JSONStore"), !1
    }
    const t = !!i.rated_at
    return !!!i.prompt_dismissed_at && !t
  }
  async recordRatingSubmitted() {
    this.update({ rated_at: new Date().toISOString() })
  }
  async recordAutomaticPromptDismissed() {
    this.update({ prompt_dismissed_at: new Date().toISOString() })
  }
}
import { ActivityPingMessage } from "../message/ping.message"
import { ModelRatedMessage } from "../message/model.message"
const p = new Logger("rating-module")
export default class ModelRatingModule extends Module {
  totalActiveTime: number
  wasLastOpeningAutomatic: boolean
  handleActivityPingMessage: (i: any) => Promise<void>
  engine: any
  store: any
  viewData: any
  handleSubmitModelRatingCommand: ({ rating: i, didFinish: t }: { rating: any; didFinish: any }) => Promise<void>
  handleToggleDialogCommand: ({ toVisible: i }: { toVisible: any }) => Promise<void>
  constructor() {
    super(...arguments),
      (this.name = "model-rating"),
      (this.totalActiveTime = 0),
      (this.wasLastOpeningAutomatic = !1),
      (this.handleActivityPingMessage = async i => {
        if (((this.totalActiveTime += i.durationDollhouse + i.durationFloorplan + i.durationInside), this.totalActiveTime < 44500)) return
        this.engine.unsubscribe(ActivityPingMessage, this.handleActivityPingMessage)
        ;(await this.store.canPromptRating()) &&
          !this.viewData.getIsDialogVisible() &&
          (this.engine.commandBinder.issueCommand(new ToggleModelRatingDialogCommand(!0)), (this.wasLastOpeningAutomatic = !0))
      }),
      (this.handleSubmitModelRatingCommand = async ({ rating: i, didFinish: t }) => {
        p.info(i),
          this.engine.broadcast(new ModelRatedMessage(i)),
          this.store.recordRatingSubmitted(),
          t &&
            (this.engine.commandBinder.issueCommand(new ToggleModelRatingDialogCommand(!1)),
            this.engine.commandBinder.issueCommand(new ToggleModalCommand(modalEnum.RATING_THANK_YOU, !0)))
      }),
      (this.handleToggleDialogCommand = async ({ toVisible: i }) => {
        this.viewData.setDialogVisible(i)
        !this.viewData.getIsDialogVisible() &&
          this.wasLastOpeningAutomatic &&
          (this.store.recordAutomaticPromptDismissed(), (this.wasLastOpeningAutomatic = !1))
      })
  }
  async init(i, t) {
    ;(this.engine = t),
      (this.viewData = new ModelRatingViewData()),
      (this.store = new ModelRatingDataStore(i.queue, i.baseUrl, i.modelId)),
      this.bindings.push(
        t.commandBinder.addBinding(ToggleModelRatingDialogCommand, this.handleToggleDialogCommand),
        t.commandBinder.addBinding(SubmitModelRatingCommand, this.handleSubmitModelRatingCommand),
        t.subscribe(ActivityPingMessage, this.handleActivityPingMessage)
      ),
      t.market.register(this, ModelRatingViewData, this.viewData)
  }
  dispose(i) {
    this.bindings.forEach(i => {
      i.cancel()
    }),
      (this.bindings = []),
      super.dispose(i)
  }
}
